package com.thor.palmmall.hdmall.common;
/**
 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 * 
 * 项目名：	cards-web
 * 文件名：	SBaseServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2017年4月19日 - liyang - 创建。
 */

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.thor.commons.exception.ThorServiceException;

/**
 * @author liyang
 * @since 0.1
 */
public abstract class HDMBaseServiceImpl {
  static final Logger logger = Logger.getLogger(HDMBaseServiceImpl.class);

  @Autowired
  @Qualifier("restTemplate")
  protected RestTemplate restTpl;
  @Autowired
  private HDMallServiceConfig config;

  /**
   * 取地服务绝对地址
   * 
   * @param path
   * @return
   */
  protected String getServiceUrl(String path) {
    return config.getBaseUrlPattern() + path;
  }

  protected <T extends HDMResponse> T get(String path, Class<T> responseType,
      Object... urlVariables) throws ThorServiceException {
    final String absUrl = getServiceUrl(path);

    ResponseEntity<T> resp = null;
    try {
      resp = restTpl.getForEntity(absUrl, responseType, urlVariables);
    } catch (HttpClientErrorException e) {
      String errMsg = e.getResponseBodyAsString();
      throw new ThorServiceException(errMsg == null ? e.getStatusText() : errMsg);
    }

    T respObj = resp.getBody();
    if (respObj.getResult() != 0) {
      throw new ThorServiceException(respObj.getMessage() == null ? "操作失败" : respObj.getMessage());
    }
    return respObj;
  }

  protected <T extends HDMResponse> T post(String path, Object request, Class<T> responseType,
      Object... urlVariables) throws ThorServiceException {
    final String absUrl = getServiceUrl(path);
    ResponseEntity<T> resp = null;
    try {
      resp = restTpl.postForEntity(absUrl, request, responseType, urlVariables);
    } catch (HttpClientErrorException e) {
      String errMsg = e.getResponseBodyAsString();
      throw new ThorServiceException(errMsg == null ? e.getStatusText() : errMsg.trim());
    }

    T respObj = resp.getBody();

    if (respObj.getResult() != 0) {
      throw new ThorServiceException(respObj.getMessage() == null ? "操作失败" : respObj.getMessage());
    }
    return respObj;
  }

  protected <T extends HDMResponse> T upload(String path, Object request, Class<T> responseType)
      throws ThorServiceException {
    final String absUrl = getServiceUrl(path);
    T resp = null;
    try {
      resp = restTpl.postForObject(absUrl, request, responseType);
    } catch (HttpClientErrorException e) {
      String errMsg = e.getResponseBodyAsString();
      throw new ThorServiceException(errMsg == null ? e.getStatusText() : errMsg.trim());
    }

    if (resp.getResult() != 0) {
      throw new ThorServiceException(resp.getMessage() == null ? "操作失败" : resp.getMessage());
    }
    return resp;
  }

  protected <T> T download(String path, Class<T> responseType, Object... urlVariables)
      throws ThorServiceException {
    final String absUrl = getServiceUrl(path);

    ResponseEntity<T> resp = null;
    try {
      resp = restTpl.getForEntity(absUrl, responseType, urlVariables);
    } catch (HttpClientErrorException e) {
      String errMsg = e.getResponseBodyAsString();
      throw new ThorServiceException(errMsg == null ? e.getStatusText() : errMsg);
    }
    return resp.getBody();
  }

}
